package com.fbee.smartcommunities.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fbee.smartcommunities.entity.Notice;
import com.fbee.smartcommunities.service.NoticeService;
import com.fbee.smartcommunities.vo.ResultVO;
import okhttp3.internal.Internal;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 汪佳泉
 * @since 2024-08-20
 */
@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Resource
    private NoticeService noticeService;

    @GetMapping("/list")
    public ResultVO<IPage<Notice>> getPage(Integer pageNo,Integer pageSize,String keyword){
        return ResultVO.success(noticeService.getPage(pageNo, pageSize, keyword));
    }

    @PostMapping("/save")
    public ResultVO<Boolean> save(String content,String title,Long[] userId){
        return ResultVO.success(noticeService.saveNotice(content,title,userId),"保存成功");
    }

    @PostMapping("/publish")
    public ResultVO<Boolean> publish(String title, Boolean state){
        return ResultVO.success(noticeService.update(new UpdateWrapper<Notice>().set("state",!state).eq("title",title)));
    }

    @GetMapping("/getById")
    public ResultVO<Notice> getById(Long id){
        return ResultVO.success(noticeService.getOne(new QueryWrapper<Notice>().eq("id",id)));
    }

    @PostMapping("/update")
    public ResultVO<Boolean> update(Long id,String content,String title){
        return ResultVO.success(noticeService.update(id,content,title),"修改成功");
    }

    @GetMapping("/check")
    public ResultVO<Boolean> check(String title){
        return ResultVO.success(noticeService.list(new QueryWrapper<Notice>().eq("title",title)).size() == 0);
    }

    @GetMapping("/getByUid")
    public ResultVO<List<Notice>> getByUid(Long openId){
        return ResultVO.success(noticeService.getByUid(openId));
    }

}
